package org.syncsoft.overalt.gui.api.componentPlugin;

import org.syncsoft.overalt.gui.api.WindowContextValue;
import org.syncsoft.overalt.gui.api.tableDataModel.AbstractTableDataModel;

/**
 * 
 * @author tptfc
 *
 */
public abstract class AbstractTableDataModelComponentPlugin<T extends AbstractTableDataModel> implements IComponentPlugin {
	/*
	 * (non-Javadoc)
	 * @see org.syncsoft.overalt.gui.swing.api.IComponentPlugin#getContext()
	 */
	@Override
	public WindowContextValue getContext() {
		WindowContextValue context = new WindowContextValue();
		buildContext(context);
		return context;
	}
	
	protected abstract Object getDataModelValue(int row, T dataModel);
	
	/**
	 * @precondition context and dataModel not are null
	 * @param context
	 * @param dataModel
	 */
	protected void addSelectedDataInContext(String key, WindowContextValue context, T dataModel) {
		if (context != null && dataModel != null) {
			int rowCount = dataModel.getRowCount();
			
			for (int i = 0; i < rowCount; i++) {
				if (dataModel.isChecked(i)) {
					context.addMultiValue(key, getDataModelValue(i, dataModel));
				}
			}
		}
	}
}
